package com.mtw.bbs.bbsUser.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mtw.bbs.bbsUser.pojo.entity.Role;
import com.mtw.bbs.bbsUser.pojo.form.RoleForm;
import com.mtw.bbs.common.core.constant.ServiceConstant;
import com.mtw.bbs.common.core.vo.Result;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.mtw.bbs.bbsUser.service.RoleService;

/**
 * 角色接口层
 * @author mo
 * @email mo
 * @date 2023-10-18 15:44:13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("admin/role")
public class RoleController {


    private final RoleService roleService;



    @ApiOperation(value = "新增角色", notes = "新增一个角色")
    @PostMapping("/save")
    public Result add(@RequestBody RoleForm form) {

        return Result.success(roleService.add(form));
    }




    @ApiOperation(value = "修改角色", notes = "修改指定角色信息")
    @PutMapping(value = "/update")
    public Result update(@RequestBody RoleForm form) {
        return Result.success(roleService.update(form));
    }



    @ApiOperation(value = "删除角色", notes = "根据url的id来指定删除对象")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable String id) {
        boolean deleted = roleService.delete(new String[]{id});
        return deleted?Result.success():Result.fail("删除失败");
    }



    @ApiOperation(value = "删除角色", notes = "根据ids来指定删除对象")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestBody String[] ids) {
        boolean deleted = roleService.delete(ids);
        return deleted?Result.success():Result.fail("删除失败");
    }




    @ApiOperation(value = "获取角色", notes = "获取指定角色信息")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable String id) {

        return Result.success(roleService.get(id));
    }



    @ApiOperation(value = "获取所有角色", notes = "获取所有角色")
    @GetMapping(value = "/getAll")
    public Result getAll() {
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Role::getStatus, ServiceConstant.STATUS_ENABLE);
        return Result.success(roleService.list(lqw));
    }



    @ApiOperation(value = "搜索角色", notes = "根据条件搜索角色信息")
    @GetMapping(value = "/page")
    public Result query(@RequestParam(name = "page") int page,
                        @RequestParam(name = "size") int size,
                        @RequestParam(name = "name",required = false) String name) {

        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(name),Role::getRoleName,name);
        lqw.orderByAsc(Role::getSort);
        return Result.success(roleService.page(new Page<>(page,size),lqw));
    }


}
